package com.tjnu.baiyi.dao;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Example;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.tjnu.baiyi.entity.TbFriend;

/**
 * @author DELL
 * 
 */
@Repository("tbFriendDao")
@Transactional
public class TbFriendDAO {
	@Autowired
	@Qualifier("sessionFactory")
	private SessionFactory sessionFactory;

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	private Session getCurrentSession() {
		return sessionFactory.getCurrentSession();
	}

	protected void initDao() {
		// do nothing
	}

	public void save(TbFriend transientInstance) {
		try {
			getCurrentSession().save(transientInstance);
		} catch (RuntimeException re) {
			throw re;
		}
	}

	public void delete(TbFriend persistentInstance) {
		try {
			getCurrentSession().delete(persistentInstance);
		} catch (RuntimeException re) {
			throw re;
		}
	}

	public void delete(Integer userId, Integer friendId) {
		try {
			String queryString = new StringBuffer()
					.append("delete from tb_friend where User_id = ? and Friend_id = ? ")
					.toString();
			Query queryObject = getCurrentSession().createSQLQuery(queryString)
					.setInteger(0, userId).setInteger(1, friendId);
			queryObject.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public TbFriend findById(java.lang.Integer id) {
		try {
			TbFriend instance = (TbFriend) getCurrentSession().get(
					"com.tjnu.baiyi.dao.TbFriend", id);
			return instance;
		} catch (RuntimeException re) {
			throw re;
		}
	}

	public List findByExample(TbFriend instance) {
		try {
			List results = getCurrentSession()
					.createCriteria("com.tjnu.baiyi.dao.TbFriend")
					.add(Example.create(instance)).list();
			return results;
		} catch (RuntimeException re) {
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		try {
			String queryString = "from TbFriend as model where model."
					+ propertyName + "= ?";
			Query queryObject = getCurrentSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			throw re;
		}
	}

	public List findAll() {
		try {
			String queryString = "from TbFriend";
			Query queryObject = getCurrentSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			throw re;
		}
	}

	/**
	 * 根据“用户Id”，查找该用户关注的所有其他用户
	 * 
	 * @return
	 */
	public List<TbFriend> findAllByUserId(Integer userId) {
		try {
			String queryString = "FROM TbFriend WHERE tbUserByUserId.userId = ?";
			Query queryObject = getCurrentSession().createQuery(queryString)
					.setInteger(0, userId);
			return queryObject.list();
		} catch (RuntimeException re) {
			throw re;
		}
	}

	/**
	 * 根据“用户Id”，查找关注该用户的用户信息
	 * 
	 * @return
	 */
	public List<TbFriend> findAllByFriendId(Integer friendId) {
		try {
			String queryString = "From TbFriend WHERE tbUserByFriendId.userId = ?";
			Query queryObject = getCurrentSession().createQuery(queryString)
					.setInteger(0, friendId);
			return queryObject.list();
		} catch (RuntimeException re) {
			throw re;
		}
	}

	/**
	 * 根据当前登录的用户Id和从前台传过来的用户Id判断“tb_friend”表中是否存在相应的记录，存在记录：返回false；不存在记录：返回true
	 * 
	 * @param friendId
	 *            好友Id
	 * @param currentUserId
	 *            当前用户Id
	 * @return
	 */
	public Boolean validateFriendByCurrentId(Integer friendId,
			Integer currentUserId) {
		String queryString = new StringBuffer()
				.append("FROM TbFriend WHERE tbUserByFriendId.userId = ? AND tbUserByUserId.userId = ?")
				.toString();
		Query queryObject = getCurrentSession().createQuery(queryString)
				.setInteger(0, friendId).setInteger(1, currentUserId);
		List<TbFriend> list = queryObject.list();
		if (list.size() == 0) {
			return true;
		} else {
			return false;
		}
	}
}